<?php
	class Model_mcart extends database{
		protected $_table = "sanpham";
		protected $_table2 = "nguyenlieu";
		protected $_table3 = "donhang";
		public function add_cart($id){
			$sql = "select * from $this->_table where masanpham = '$id'";
			$this->query($sql);
			return $this->fetch();
		}
		public function total_cart(){
			$sql = "select * from $this->_table";
			$this->query($sql);
			return $this->num_rows();
		}
		public function list_cart(){
			$sql = "select * from $this->_table";
			$this->query($sql);
			return $this->fetchAll();
		}
		public function del_cart($id){
			if($_GET['type']){
				if($_GET['type'] == 'one'){
					unset($_SESSION['cart'][$id]);
					
				}else{
					//session_destroy();
					unset($_SESSION['cart']);
				}
			}
		}
		public function del_cart2($id){
			if($_GET['type']){
				if($_GET['type'] == 'one'){
					unset($_SESSION['cart2'][$id]);
					
				}else{
					//session_destroy();
					unset($_SESSION['cart2']);
				}
			}
		}
		public function del_cart3($id){
			if($_GET['type']){
				if($_GET['type'] == 'one'){
					unset($_SESSION['cart3'][$id]);
					
				}else{
					//session_destroy();
					unset($_SESSION['cart3']);
				}
			}
		}
		public function del_cart4($id){
			if($_GET['type']){
				if($_GET['type'] == 'one'){
					unset($_SESSION['cart4'][$id]);
					
				}else{
					//session_destroy();
					unset($_SESSION['cart4']);
				}
			}
		}
		public function update_cart(){
			$stt = 0;
			foreach($_POST['cart_num'] as $k){
				if($_POST['cart_qty'][$stt] == 0){
					//unset($_SESSION['cart'][$k]);
				}else{
					if(is_numeric($_POST['cart_qty'][$stt])){
						if($_POST['cart_qty'][$stt] > 0){
							$_SESSION['cart'][$k]['qty'] = $_POST['cart_qty'][$stt];
						}else{
							
						}
					}
				}
				$stt++;
			}
		}
		public function getdata($id){
			$sql = "select * from $this->_table where masanpham = '$id'";
			$this->query($sql);
			return $this->fetch();
		}
		
		public function add_cart2($id){
			//$sql = "select * from $this->_table2 where manguyenlieu = '$id'";
			$sql = "select * from $this->_table2 inner join nhacungcap on nhacungcap.manhacungcap = nguyenlieu.manhacungcap where nguyenlieu.manguyenlieu = '$id'";
			$this->query($sql);
			return $this->fetch();
		}
		
		public function update_all_cart($data,$id){
			foreach($data as $k => $v){
				$arr[] = "$k = '$v'";
			}
			$col = implode(",",$arr);
			$sql = "update $this->_table3 set $col where id = '$id'";
			$this->query($sql);
		}
	}
?>